package com.weimob.mcloud.wechatwork.customer.service.check;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.service.user.exception.BizException;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author junlong.lv
 * @version 1.0
 * @date 2021/12/21 1:24 下午
 */
@Component
@Slf4j
public class CommonCheckService {

    @Reference
    private PrivateSphereService privateSphereService;

    public void checkPrivateSphere(PrivateSphere privateSphere) {
        if (Objects.isNull(privateSphere)) {
            throw new BizException("私域信息不能为空");
        }

        if (Objects.nonNull(privateSphere.getBosId())
                && StringUtils.isNotBlank(privateSphere.getCorpId())
                && StringUtils.isNotBlank(privateSphere.getPrivateSphereId())
                && Objects.nonNull(privateSphere.getProductInstanceId())) {
            return;
        }


        SoaResponse<PrivateSphere, Void> soaResponse = privateSphereService.simpleFetch(privateSphere);
        if (0L != Long.parseLong(soaResponse.getReturnCode())) {
            log.error("查询私域失败, request:{}, error:{}", JSON.toJSONString(privateSphere), soaResponse.getReturnMsg());
            throw new BizException(Long.valueOf(soaResponse.getReturnCode()), soaResponse.getReturnMsg());
        }

        privateSphere = soaResponse.getResponseVo();
        if (Objects.isNull(privateSphere)) {
            throw new BizException("私域信息没查到");
        }
    }
}
